﻿/*
* 注意：搜索【TODO】填写未完成的部分！
*/

(function () {

	//-------- 一些常量

	var i_DftAnmtDur = 300;

	//-------- API

	(function () {

		/// 根据模式调整索引，若失败返回null
		nApp.fAjsIdxByMode = function (a_Ary, a_Idx, a_Mode) {
			if ((a_Idx < 0) || (a_Ary.length <= a_Idx)) {
				if (-1 == a_Mode) {
					return null;
				}
				else
					if (0 == a_Mode) {
						if (a_Idx < 0) {
							a_Idx = a_Ary.length - 1;
						}
						else {
							a_Idx = 0;
						}
					}
					else
						if (+1 == a_Mode) {
							a_Idx = nWse.stNumUtil.cClmOnAry(null, a_Ary, a_Idx);
						}
						else
							if (+2 == a_Mode) {
								//throw new Error("无法处理！");
								return null;
							}
			}
			return a_Idx;
		};

		// tPctrCarsl，图片轮播（Picture Carousel）
		(function () {
			nWse.fClass(nApp,
				/// a_Cfg：Object
				/// {
				/// c_Self：HTMLElement，自身元素，必须有效
				/// c_OnCrtDotBtn：void f(a_This, a_DotBtn, a_BtnIdx)，当创建圆点按钮时
				/// }
				function tPctrCarsl(a_Cfg) {
					var l_This = this;
					l_This.e_Cfg = a_Cfg;
					l_This.e_Self = l_This.e_Cfg.c_Self;
					l_This.e_Self.App_Ctrl = l_This;
					l_This.e_Mode = parseInt(l_This.e_Self.getAttribute("data-mode"));	// 模式，-1=忽略，0=环绕（默认），+1=截断，2=折回
					if (+2 == l_This.e_Mode) {
						l_This.e_Mode_Fowd = true;	// 正向
					}

					//	console.log(l_This.e_Self.getAttribute("abc")); // 没有的特性返回null，无值的特性返回空串

					// 取得所有图像，设置z-index，赋予CSS背景属性
					l_This.e_Pctrs = nWse.stDomUtil.cQryAll("[data-bkgd_img_src]", l_This.e_Self);
					if (0 == l_This.e_Pctrs.length) {
						return;
					}

					nWse.stAryUtil.cFor(l_This.e_Pctrs,
						function (a_Divs, a_DivIdx, a_Div) {
							a_Div.style.zIndex = (a_Divs.length - 1 - a_DivIdx).toString(); // 倒序

							// 取得URL，跳过不存在的，【注意】允许不提供值，表示不用加载背景图像，但仍进行交互和呈现处理
							var l_Url = a_Div.getAttribute('data-bkgd_img_src');
							if (!l_Url) {
								return;
							}
							a_Div.style.backgroundImage = 'url(' + l_Url + ')';
						});

					// 圆点按钮
					if (l_This.e_Self.getAttribute("data-dot_btns_slctr")) {
						l_This.e_DotBtns = nWse.stDomUtil.cQryAll(l_This.e_Self.getAttribute("data-dot_btns_slctr"), l_This.e_Self);
					}
					else
						if (l_This.e_Self.getAttribute("data-dot_btns_prn_slctr")) {
							(function () {
								var l_Prn = nWse.stDomUtil.cQryOne(l_This.e_Self.getAttribute("data-dot_btns_prn_slctr"), l_This.e_Self);
								l_This.e_DotBtns = new Array(l_This.e_Pctrs.length);
								nWse.stAryUtil.cFor(l_This.e_Pctrs,
									function (a_Divs, a_DivIdx, a_Div) {
										var l_DotBtn = l_This.e_DotBtns[a_DivIdx] = document.createElement("div");
										if (l_This.e_Cfg.c_OnCrtDotBtn) {
											l_This.e_Cfg.c_OnCrtDotBtn(l_This, l_DotBtn, a_DivIdx);
										}
										l_Prn.appendChild(l_DotBtn);
									});
							})();
						}

					if (l_This.e_DotBtns) {
						nWse.stAryUtil.cFor(l_This.e_DotBtns,
							function (a_DotBtns, a_BtnIdx, a_Btn) {
								// 单击或鼠标移入时激活
								function fHdlIpt() {
									l_This.cActv(a_BtnIdx);
									l_This.cRtmAutoPlay(true);
								}

								nWse.stDomUtil.cAddEvtHdlr(a_Btn, "click", fHdlIpt);
								nWse.stDomUtil.cAddEvtHdlr(a_Btn, "mouseover", fHdlIpt);
							});
					}

					// 箭头按钮
					if (l_This.e_Self.getAttribute("data-arw_btn_slctr")) {
						(function () {
							var l_ArwBtns = nWse.stDomUtil.cQryAll(l_This.e_Self.getAttribute("data-arw_btn_slctr"), l_This.e_Self);
							if (l_ArwBtns.length != 2) {
								return;
							}

							if (nWse.stCssUtil.cHasCssc(l_ArwBtns[0], "mi_lt")) {
								l_This.e_LtArwBtn = l_ArwBtns[0];
								l_This.e_RtArwBtn = l_ArwBtns[1];
							}
							else {
								l_This.e_LtArwBtn = l_ArwBtns[1];
								l_This.e_RtArwBtn = l_ArwBtns[0];
							}

							nWse.stDomUtil.cAddEvtHdlr(l_This.e_LtArwBtn, "click",
								function () {
									l_This.cActv(l_This.cGetActvIdx() - 1);
									l_This.cRtmAutoPlay(true);
								});
							nWse.stDomUtil.cAddEvtHdlr(l_This.e_RtArwBtn, "click",
								function () {
									l_This.cActv(l_This.cGetActvIdx() + 1);
									l_This.cRtmAutoPlay(true);
								});

							function fNoSlc(a_Evt) {
								nWse.stDomUtil.cPvtDftEvtActn(a_Evt).cStopPpgtEvt(a_Evt);
								return false;
							}
							nWse.stDomUtil.cAddEvtHdlr(l_This.e_LtArwBtn, "selectstart", fNoSlc);
							nWse.stDomUtil.cAddEvtHdlr(l_This.e_RtArwBtn, "selectstart", fNoSlc);
						})();
					}

					// 如果自动播放
					l_This.e_AutoPlayPrid = null;
					if (l_This.e_Self.getAttribute("data-auto_play_prid")) {
						l_This.e_AutoPlayPrid = parseFloat(l_This.e_Self.getAttribute("data-auto_play_prid"));
						l_This.cRtmAutoPlay();
					}

					// 动画？
					l_This.e_Anmt = null;
					if (l_This.e_Self.getAttribute("data-anmt")) {
						l_This.e_Anmt = l_This.e_Self.getAttribute("data-anmt");
						l_This.eAnmtInit();
					}

					// 默认激活首个
					l_This.cActv(0);
				},
				null,
				{
					cAcsSelf: function () {
						return this.e_Self;
					}
					,
					/// 激活
					cActv: function (a_ActvIdx) {
						var l_This = this;
						a_ActvIdx = nApp.fAjsIdxByMode(l_This.e_Pctrs, a_ActvIdx, l_This.e_Mode);
						if (null === a_ActvIdx) {
							return false;
						}

						// 调整z-index和CSS
						var l_ActvDiv = l_This.e_Pctrs[a_ActvIdx];
						var l_ActvDivOldZidx = parseInt(l_ActvDiv.style.zIndex);
						nWse.stAryUtil.cFor(l_This.e_Pctrs,
							function (a_Divs, a_DivIdx, a_Div) {
								// 如果更大，降一级
								var l_Zidx = parseInt(a_Div.style.zIndex);
								if (l_Zidx > l_ActvDivOldZidx) {
									a_Div.style.zIndex = (l_Zidx - 1).toString(); // 倒序
								}

								// CSS类
								if (a_DivIdx == a_ActvIdx) {
									nWse.stCssUtil.cAddCssc(a_Div, "mi_actv");
								}
								else {
									nWse.stCssUtil.cRmvCssc(a_Div, "mi_actv");
								}

								// 停止动画
								nWse.stCssUtil.cFnshAnmt(l_ActvDiv, false, false, false);
							});

						l_ActvDiv.style.zIndex = (l_This.e_Pctrs.length - 1).toString(); // 总是对应最后一个索引

						// 动画
						l_This.eAnmtRun(a_ActvIdx);

						// 如果有箭头，且模式非0，调整CSS
						if (l_This.e_LtArwBtn && (0 != l_This.e_Mode)) {
							if (0 == a_ActvIdx) {
								nWse.stCssUtil.cAddCssc(l_This.e_LtArwBtn, "mi_lmt");
								nWse.stCssUtil.cRmvCssc(l_This.e_RtArwBtn, "mi_lmt");
							}
							else
								if (l_This.e_Pctrs.length - 1 == a_ActvIdx) {
									nWse.stCssUtil.cRmvCssc(l_This.e_LtArwBtn, "mi_lmt");
									nWse.stCssUtil.cAddCssc(l_This.e_RtArwBtn, "mi_lmt");
								}
								else {
									nWse.stCssUtil.cRmvCssc(l_This.e_LtArwBtn, "mi_lmt");
									nWse.stCssUtil.cRmvCssc(l_This.e_RtArwBtn, "mi_lmt");
								}
						}

						// 如果有圆点按钮，调整CSS
						if (l_This.e_DotBtns) {
							nWse.stAryUtil.cFor(l_This.e_DotBtns,
								function (a_Btns, a_BtnIdx, a_Btn) {
									if (a_BtnIdx == a_ActvIdx) {
										nWse.stCssUtil.cAddCssc(a_Btn, "mi_actv");
									}
									else {
										nWse.stCssUtil.cRmvCssc(a_Btn, "mi_actv");
									}
								});
						}
						return true;
					}
					,
					eAnmtInit: function () {
						var l_This = this;

						// 如果不需要动画
						if ((!l_This.e_Anmt) || (0 == l_This.e_Pctrs.length)) {
							return;
						}

						// 根据e_Anmt动画
						if ("tAph" == l_This.e_Anmt) {
							(function () {
								(function () {
									l_This.e_AnmtData = {};

									l_This.e_AnmtData.vcRun = function (a_ActvIdx) {
										var l_ActvDiv = l_This.e_Pctrs[a_ActvIdx];
										if (1 == l_This.e_Pctrs.length) {
											l_ActvDiv.style.opacity = "1";
											return;
										}

										if (!nWse.stCssUtil.cIsDurAnmt(l_ActvDiv)) {
											l_ActvDiv.style.opacity = "0";
										}

										nWse.stCssUtil.cAnmt(l_ActvDiv, "",
											{
												"opacity": 1
											},
											{
												c_Dur: 0.6,
												c_fEsn: nWse.stNumUtil.cEsn_FastToSlow
											});
									};
								})();
							})();
						}
						else
							if ("tHrztSld" == l_This.e_Anmt) {
								(function () {
									l_This.e_AnmtData = {};
									l_This.e_AnmtData.c_Ctanr = l_This.e_Pctrs[0].parentNode;
									//	console.log(l_This.e_Ctanr.offsetWidth);

									l_This.e_AnmtData.cUpdItemMgn = function (a_Item) {
										if (!a_Item.App_Mgn) {
											a_Item.App_Mgn = {};
										}
										nWse.stCssUtil.cGetMgn(a_Item.App_Mgn, a_Item);
									};

									l_This.e_AnmtData.cCalcItemTotWid = function (a_Item) {
										l_This.e_AnmtData.cUpdItemMgn(a_Item);
										var l_Mgn = a_Item.App_Mgn;
										return a_Item.offsetWidth + l_Mgn.c_MgnLt + l_Mgn.c_MgnRt;
									};

									l_This.e_AnmtData.cCalcActvX = function (a_TotWid) {
										return Math.max(0, (l_This.e_AnmtData.c_Ctanr.offsetWidth - a_TotWid) / 2);
									};

									l_This.e_AnmtData.cUpdItemX = function (a_ActvIdx, a_ImdtUpd) {
										var l_Items = l_This.e_Pctrs;
										var l_ActvItem = l_Items[a_ActvIdx];
										l_ActvItem.App_TotWid = l_This.e_AnmtData.cCalcItemTotWid(l_ActvItem);
										var l_ActvW = l_ActvItem.App_TotWid;
										var l_ActvX = l_This.e_AnmtData.cCalcActvX(l_ActvW);
										l_ActvItem.App_X = l_ActvX;

										var l_AccW = 0;
										var i, l_Len = l_Items.length;
										for (i = a_ActvIdx - 1; i >= 0; --i) {
											l_Items[i].App_TotWid = l_This.e_AnmtData.cCalcItemTotWid(l_Items[i]);
											l_AccW += l_Items[i].App_TotWid;
											l_Items[i].App_X = l_ActvX - l_AccW;
										}

										l_AccW = l_ActvW;
										for (i = a_ActvIdx + 1; i < l_Len; ++i) {
											l_Items[i].App_X = l_ActvX + l_AccW;
											l_Items[i].App_TotWid = l_This.e_AnmtData.cCalcItemTotWid(l_Items[i]);
											l_AccW += l_Items[i].App_TotWid;
										}

										// 循环？
										if (0 == l_This.e_Mode) {
											if ((l_Len >= 3)) {
												if ((0 == a_ActvIdx)) {
													l_Items[l_Len - 1].App_X = l_Items[0].App_X - l_Items[l_Len - 1].App_TotWid;

													// 从背后立即跳到目标位置
													if (a_ImdtUpd) {
														nWse.stCssUtil.cFnshAnmt(l_Items[l_Len - 1]);
														l_Items[l_Len - 1].style.left = Math.round(l_Items[l_Len - 1].App_X) + "px";
													}
												}
												else
												if ((l_Len - 1 == a_ActvIdx)) {
													l_Items[0].App_X = l_Items[l_Len - 1].App_X + l_Items[l_Len - 1].App_TotWid;

													// 从背后立即跳到目标位置
													if (a_ImdtUpd) {
														nWse.stCssUtil.cFnshAnmt(l_Items[0]);
														l_Items[0].style.left = Math.round(l_Items[0].App_X) + "px";
													}
												}
											}
											else
												if (l_Len == 2) {
													if (1 == a_ActvIdx) {
														l_Items[0].App_X = l_Items[1].App_X + l_Items[1].App_TotWid;
														if (a_ImdtUpd) {
															nWse.stCssUtil.cFnshAnmt(l_Items[0]);
															l_Items[0].style.left = Math.round(l_Items[0].App_X) + "px";
														}
													}
													else {
														l_Items[1].App_X = l_Items[0].App_X + l_Items[0].App_TotWid;
														if (a_ImdtUpd) {
															nWse.stCssUtil.cFnshAnmt(l_Items[1]);
															l_Items[1].style.left = Math.round(l_Items[1].App_X) + "px";
														}
													}
												}
										}
									};

									l_This.e_AnmtData.cRgltPos = function (a_ActvIdx) {
										l_This.e_AnmtData.cUpdItemX(a_ActvIdx);
										nWse.stAryUtil.cFor(l_This.e_Pctrs,
											function (a_Items, a_ItemIdx, a_Item) {
												a_Item.style.left = Math.round(a_Item.App_X) + "px";
											});
									};

									l_This.e_AnmtData.vcRun = function (a_ActvIdx) {
										var l_Items = l_This.e_Pctrs;
										var l_Len = l_Items.length;
										var l_ActvItem = l_This.e_Pctrs[a_ActvIdx];
										l_This.e_AnmtData.cUpdItemX(a_ActvIdx, false);	// 不要立即更新

										nWse.stAryUtil.cFor(l_Items,
										function (a_Items, a_ItemIdx, a_Item) {
											var l_Lt = Math.round(a_Item.App_X) + "px";
											//	a_Item.style.left = l_Lt;

											// 动画
											nWse.stCssUtil.cAnmt(a_Item, "",
											{
												"left": l_Lt
											},
											{
												c_Dur: 0.4,
												c_fEsn: nWse.stNumUtil.cEsn_FastToSlow
											});
										});
									};

									// 窗口调整大小时，重算位置
									nWse.stDomUtil.cAddEvtHdlr_WndRsz(function () {
										l_This.e_AnmtData.cRgltPos(l_This.cGetActvIdx());
									}, 1 / 10);

									// 初始激活[0]
									l_This.e_AnmtData.cRgltPos(0);
								})();
							}
					}
					,
					eAnmtRun: function (a_ActvIdx) {
						var l_This = this;

						// 如果不需要动画
						if ((!l_This.e_Anmt) || (0 == l_This.e_Pctrs.length)) {
							return;
						}

						// 运行
						l_This.e_AnmtData.vcRun(a_ActvIdx);
					}
					,
					cGetActvIdx: function () {
						var l_This = this;
						return nWse.stAryUtil.cFind(l_This.e_Pctrs,
							function (a_Divs, a_DivIdx, a_Div) {
								var l_Zidx = parseInt(a_Div.style.zIndex);
								return (l_Zidx == l_This.e_Pctrs.length - 1);
							});
					}
					,
					cRtmAutoPlay: function (a_WaitMore) {
						var l_This = this;
						if (!l_This.e_AutoPlayPrid) {
							return this;
						}

						if (l_This.e_AutoPlayTmrId) {
							clearTimeout(l_This.e_AutoPlayTmrId);
							l_This.e_AutoPlayTmrId = null;
						}

						function fAutoPlay() {
							var l_Idx = l_This.cGetActvIdx();
							if (l_This.e_Pctrs.length > 1) {
								if (+2 == l_This.e_Mode) {
									if (l_This.e_Mode_Fowd) {
										if (l_Idx >= l_This.e_Pctrs.length - 1) {
											l_Idx = l_This.e_Pctrs.length - 2;
											l_This.e_Mode_Fowd = false;
										}
										else {
											l_Idx = l_Idx + 1;
										}
									}
									else {
										if (l_Idx <= 0) {
											l_Idx = 1;
											l_This.e_Mode_Fowd = true;
										}
										else {
											l_Idx = l_Idx - 1;
										}
									}
								}
								else {
									l_Idx = l_Idx + 1;
								}
							}

							l_This.cActv(l_Idx);
							l_This.e_AutoPlayTmrId = setTimeout(fAutoPlay, l_This.e_AutoPlayPrid * 1000);
						}

						var l_WaitTime = l_This.e_AutoPlayPrid * 1000;
						if (a_WaitMore) {
							l_WaitTime *= 10;
						}
						l_This.e_AutoPlayTmrId = setTimeout(fAutoPlay, l_WaitTime);
						return this;
					}
				});
		})();

	})();

	//-------- 文档就绪	

	nWse.stPageInit.cAddEvtHdlr_DocRdy(function () {
		var i_DftAnmtDur = 300;

		// 导航按钮
		(function () {

			var s_NavDiv = document.getElementById("k_NavDiv");
			var s_NavBtn = document.getElementById("k_NavBtn");
			nWse.stDomUtil.cAddEvtHdlr(s_NavBtn, "click",
				function () {
					//	console.log("click nav")

					s_NavDiv.style.display = s_NavDiv.style.display ? "" : "block";
				});

		})();

	})();
})();


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////